/**
 * 
 */
package com.demo2do.boyle.backend.web.controller.aduser;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.aduser.AduserRefundService;
import com.demo2do.boyle.common.entity.aduser.refund.AduserRefund;
import com.demo2do.boyle.common.entity.aduser.refund.RefundStatus;
import com.demo2do.boyle.common.entity.system.Principal;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.demo2do.core.web.resolver.Secure;

/**
 * @author Leo
 *
 */
@Controller
@RequestMapping("/aduser-refunds")
@MenuSetting("menu-aduser")
public class AduserRefundController {

	@Autowired
	private AduserRefundService aduserRefundService;
	
	/**
     * list all aduserRefunds
     * 
     * @return
     */
    @RequestMapping("")
    @MenuSetting("submenu-aduser-refund")
    public ModelAndView list(@RequestParam(value = "status",required = false) RefundStatus status,Page page) {
        
    	ModelAndView modelAndView = new ModelAndView("aduser/refund/aduser-refund-list");

    	int count = aduserRefundService.count(status);
    	modelAndView.addObject("count", count);
        modelAndView.addObject("status", status); 
    	
        if(count > 0) {
        	List<AduserRefund> aduserRefunds = aduserRefundService.list(status, page);
        	modelAndView.addObject("aduserRefunds", aduserRefunds);
        }
        
        
        return modelAndView;
        
    }
	
    /**
     * display the audit panel
     * 
     * @param id
     * @return the view to confirm the submit
     */
    @RequestMapping("/{id}/audit")
    public ModelAndView audit(@PathVariable("id") Integer id) {
    	ModelAndView modelAndView = new ModelAndView("aduser/refund/aduser-refund-audit");
		modelAndView.addObject("id", id);
		return modelAndView;
    }
    
    /**
     * do audit 
     * 
     * @param id
     * @return the audit result with message
     */
    @RequestMapping(value ="/{id}/audit", method = RequestMethod.PATCH )
    public @ResponseBody String onAudit(@PathVariable("id") Integer id, @Secure Principal principal) {
    	Result result = new Result();
    	try{
    		
    		AduserRefund aduserRefund = aduserRefundService.load(AduserRefund.class, id);
    		
    		aduserRefundService.audit(aduserRefund.operator(principal.getName()));
    		
        	result.success().message("审核成功");
    	}catch(Exception e){
    		result.fail().message("审核失败");
    	}
    	return JsonUtils.toJsonString(result);
    }
}
